package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.AdminSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Slf4j
@Service
@Transactional
public class AdminSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements AdminSensitiveService {


    /**
     * 查询敏感词列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(SensitiveDto dto) {
        // 需求
        // 1. 查询需要按照创建时间倒序查询
        // 2. 按照敏感词名称模糊查询
        // 3. 分页查询

        // 1. 检查参数
        dto.checkParam();

        // 2. 分页查询
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        // 2.1 按照频道名称模糊查询
        query.like(dto.getName()!=null, WmSensitive::getSensitives, dto.getName());
        // 2.2 按照创建时间倒序查询
        query.orderByDesc(WmSensitive::getCreatedTime);
        // 2.3 查询结果
        page(page, query);

        // 3. 返回结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    /**
     * 新增敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        // 需求
        // 1. 弹出的输入框，输入敏感词可直接保存
        // 2. 已存在的敏感词则不能保存

        // 1. 检查参数
        if(StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "敏感词不能为空");
        }

        // 2. 判断敏感词名称是否重复
        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();
        query.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        query.last("limit 1");
        WmSensitive sensitive = getOne(query);
        if(sensitive != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        // 3. 新增敏感词
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delSensitive(Integer id) {
        // 需求
        // 1. 直接删除即可

        // 1. 检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 根据id查询敏感词
        WmSensitive wmSensitive = getById(id);
        if(wmSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 3. 删除敏感词
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        // 需求
        // 1. 直接修改

        // 1. 检查参数
        if(StringUtils.isBlank(wmSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2. 修改敏感词
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
